<?php
/**
 * Created by PhpStorm.
 * User: JYuans
 * Date: 2019/12/10
 * Time: 17:27
 */
namespace Bstage\Controller;

class TeamExamineNewPromotionController extends BstageController {

    /**
     * 设置班子考评新提拔人员接口
     */
    public function setNewPromotionAction(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        $team_examine_data = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("status")
            ->find();
        if(!$team_examine_data){
            $this->errorReturn("班子考评ID错误");
        }

        if($team_examine_data['status'] != 0){
            $this->errorReturn("该考评无法重新设置");
        }

        $new_promotion_json = $_REQUEST['new_promotion_json']?$_REQUEST['new_promotion_json']:$this->errorReturn("缺少新提拔人员json");

        $new_promotion_array = json_decode($new_promotion_json,true);

        $model = M();
        $model->startTrans();

        M("TeamExamineNewPromotion")
            ->where(['team_examine_id'=>$team_examine_id])
            ->delete();

        $new_p_num = 1;
        foreach($new_promotion_array as $key=>$value){
            $new_data = array();
            $new_data['team_examine_id'] = $team_examine_id;
            $new_data['new_p_num'] = $new_p_num;
            $new_data['team_id'] = $value['team_id'];
            $new_data['name'] = $value['name'];
            $new_data['date_birth'] = strtotime($value['date_birth']);
            $new_data['former_duties'] = $value['former_duties'];
            $new_data['now_duties'] = $value['now_duties'];
            $new_data['appointment_time'] = strtotime($value['appointment_time']);

            $add_result = M("TeamExamineNewPromotion")
                ->add($new_data);
            if(!$add_result){
                $model->rollback();
                $this->errorReturn("添加失败，请重新尝试");
            }
            $new_p_num++;
        }
        $model->commit();
        $this->successReturn("保存成功");
    }

    /**
     * 获取新提拔人员列表列表接口
     */
    private function getNewPromotionList(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        $new_promotion_list = M("TeamExamineNewPromotion")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("new_p_id,team_id,name,date_birth,former_duties,now_duties,appointment_time,add_time")
            ->select();
        if(!$new_promotion_list){
            $this->returnJson(503,"无数据");
        }

        $team_id_array = array_column($new_promotion_list,"team_id");
        $team_name_array = M("Team")
            ->where(['team_id'=>['in',$team_id_array]])
            ->getField("team_id,name");
        if(!$team_name_array){
            $team_name_array = array();
        }

        foreach($new_promotion_list as $key=>$value){
            $new_promotion_list[$key]['date_birth'] = date("Y-m",$value['date_birth']);
            $new_promotion_list[$key]['appointment_time'] = changeDate($value['appointment_time']);
            $new_promotion_list[$key]['add_time'] = changeDateTime($value['add_time']);

            $new_promotion_list[$key]['team_name'] = $team_name_array[$value['team_id']];
        }

        $this->successReturn("获取成功",$new_promotion_list);
    }

    /**
     * 添加新提拔人员接口
     */
    private function addNewPromotionAction(){
        //team_examine_id,team_id,name,date_birth,former_duties,now_duties,appointment_time,add_time
        $new_data = array();
        $new_data['team_examine_id'] = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");
        $new_data['team_id'] = $_REQUEST['team_id']?$_REQUEST['team_id']:$this->errorReturn("缺少班子ID");
        $new_data['name'] = $_REQUEST['name']?$_REQUEST['name']:$this->errorReturn("请输入姓名");
        $new_data['date_birth'] = $_REQUEST['date_birth']?$_REQUEST['date_birth']:$this->errorReturn("请输入出生年月");
    }

}